$(function() {
	//初始化
	var loginInfo = JSON.parse(localStorage.getItem("loginInfo")); //获取当前用户信息
	var storesArr = []; //存储stores所有uuid
	function init() {
		var mydate = new Date();
		var year = mydate.getFullYear();
		var month = mydate.getMonth() + 1;
		if(parseInt(month) < 10) {
			month = '0' + month;
		}
		$('#fromYear,#toYear').html(year);
		$('#fromMonth,#toMonth').html(month);

		for(var i = 0; i < loginInfo.stores.length; i++) {
			$('.navListContent ul.ul1').append('<li><span>' + loginInfo.stores[i].name + '</span><i></i></li>');
			storesArr[i] = loginInfo.stores[i].uuid;

		}
		searchBill(storesArr); //进入页面直接加载
	}
	init();

	//导航列表点击事件
	var liArr = [];//存储列表被选中索引
	$('.nav li').click(function() {
		var reg = new RegExp('on');
		if(!reg.test($(this).attr('class'))) {
			$(this).addClass('on').siblings().removeClass('on');
			$(this).children('img').attr('src', '../pages/bill/img/arrow_dwon_gray.png');
			$(this).siblings().children('img').attr('src', '../pages/bill/img/arrow_right_gray.png');
			$('.navListContent').show();
			$('.display').hide();
		} else {
			$(this).children('img').attr('src', '../pages/bill/img/arrow_right_gray.png');
			$(this).siblings().children('img').attr('src', '../pages/bill/img/arrow_right_gray.png');
			$(this).removeClass('on');
			$('.navListContent').hide();
			$('.display').show();
		}
		var index = $(this).index();
		$('.navListContent ul').hide();
		$('.navListContent ul').eq(index).show();

		if(index == 1) { //点击全部业态
			yetai();
			console.log(liArr[0])
			if(liArr[0]){
				$('.navListContent ul.ul2 li').eq(liArr[0]).children('i').addClass('on');
			}
		} else if(index == 2) { //点击全部楼宇
			louyu(storesT);
			if(liArr[1]){
				$('.navListContent ul.ul3 li').eq(liArr[1]).children('i').addClass('on');
			}
		} else if(index == 3) { //点击全部楼层
			louceng(building, storesT);
			if(liArr[2]){
				$('.navListContent ul.ul4 li').eq(liArr[2]).children('i').addClass('on');
			}
		}
	})
	//下拉选项添加点击事件
	var building = null; //需要传入楼层的楼宇参数
	var storesT = []; //需要当做参数stores

	$('.navListContent ul').on('click', 'li', function() {
		var index = $(this).index();
		$(this).children('i').addClass('on');
		$(this).siblings().children('i').removeClass('on');

		var ul1 = new RegExp('ul1'); //全部项目
		var ul2 = new RegExp('ul2'); //全部业态
		var ul3 = new RegExp('ul3'); //全部楼宇
		var ul4 = new RegExp('ul4'); //全部楼宇

		if(ul1.test($(this).parent().attr('class')) && index != 0) { //点击全部项目第一行
			storesT[0] = storesArr[index - 1]; //将单项数据UUID传递
		} else if(ul1.test($(this).parent().attr('class')) && index == 0) {
			storesT = storesArr; //传递所有UUID
		}

		//判断是否点击列表,将列表中的文字替换标题
		var keywords = null; //搜索关键字
		if(ul3.test($(this).parent().attr('class'))) {
			building = louyuArr[index - 1];
			keywords = $(this).children('span').text();
			$('.nav li').eq(2).find('span').text(keywords);
			if(keywords == '全部楼宇') {
				keywords = null;
			}
			liArr[1] = index;
			layer.load(2);
			setTimeout(function() {
				layer.closeAll('loading');
				searchBill(storesArr, keywords);
				$('.nav li').eq(2).children('img').attr('src', '../pages/bill/img/arrow_right_gray.png');
				$('.nav li').eq(2).removeClass('on');
				$('.navListContent').hide();
				$('.display').show();
			}, 500);
		} else if(ul1.test($(this).parent().attr('class'))) {
			keywords = $(this).children('span').text();
			$('.nav li').eq(0).find('span').text(keywords);
			if(keywords == '全部项目') {
				keywords = null;
			}
			layer.load(2);
			setTimeout(function() {
				layer.closeAll('loading');
				searchBill(storesArr, keywords);
				$('.nav li').eq(0).children('img').attr('src', '../pages/bill/img/arrow_right_gray.png');
				$('.nav li').eq(0).removeClass('on');
				$('.navListContent').hide();
				$('.display').show();
			}, 500);
		} else if(ul2.test($(this).parent().attr('class'))) {
			keywords = $(this).children('span').text();
			$('.nav li').eq(1).find('span').text(keywords);
			if(keywords == '全部业态') {
				keywords = null;
			}
			liArr[0] = index;
			layer.load(2);
			setTimeout(function() {
				layer.closeAll('loading');
				searchBill(storesArr, keywords);
				$('.nav li').eq(1).children('img').attr('src', '../pages/bill/img/arrow_right_gray.png');
				$('.nav li').eq(1).removeClass('on');
				$('.navListContent').hide();
				$('.display').show();
			}, 500);
		} else if(ul4.test($(this).parent().attr('class'))) {
			keywords = $(this).children('span').text();
			$('.nav li').eq(3).find('span').text(keywords);
			if(keywords == '全部楼层') {
				keywords = null;
			}
			liArr[2] = index;
			layer.load(2);
			setTimeout(function() {
				layer.closeAll('loading');
				searchBill(storesArr, keywords);
				$('.nav li').eq(3).children('img').attr('src', '../pages/bill/img/arrow_right_gray.png');
				$('.nav li').eq(3).removeClass('on');
				$('.navListContent').hide();
				$('.display').show();
			}, 500);
		}
	})

	//---------------------查询业态
	function yetai() {
		var yetaiArr = []; //存储业态所有数据
		$.ajax({
			url: "getBizType.thor",
			cache: false,
			dataType: 'json', //服务器返回json格式数据
			type: 'post', //HTTP请求类型
			timeout: 10000, //超时时间设置为10秒；
			success: function(data) {
				var len = data.length;
				$('.navListContent ul.ul2 li').not($('.navListContent ul.ul2 li:first')).empty().remove();
				for(var i = 0; i < len; i++) {
					$('.navListContent ul.ul2').append('<li><span>' + data[i].name + '</span><i></i></li>');
					yetaiArr[i] = data[i];

				}
			},
			error: function(data) {
				var data = JSON.stringify(data)
				layer.msg("查询失败")
			}
		});
	}
	//---------------------查询业态
	//---------------------------查询楼宇
	var louyuArr = []; //存储楼宇所有数据
	function louyu(storesArr) {
		var buildParam = {};
		buildParam.stores = storesArr;

		$.ajax({
			url: "getBuildings.thor",
			cache: false,
			data: {
				buildParam: JSON.stringify(buildParam)
			},
			dataType: 'json', //服务器返回json格式数据
			type: 'post', //HTTP请求类型
			timeout: 10000, //超时时间设置为10秒；
			success: function(data) {
				$('.navListContent ul.ul3 li').not($('.navListContent ul.ul3 li:first')).empty().remove();
				var len = data.length;
				for(var i = 0; i < len; i++) {
					$('.navListContent ul.ul3').append('<li><span>' + data[i].name + '</span><i></i></li>');
					louyuArr[i] = data[i];
				}
			},
			error: function(data) {
				var data = JSON.stringify(data)
				layer.msg("查询失败")
			}
		});
	}
	//---------------------------查询楼宇
	//-------------------------查询楼层
	function louceng(building, storesArr) {
		var louyengArr = []; //存储楼层所有数据
		var floorParam = {};
		floorParam.stores = storesArr;
		floorParam.building = building;

		$.ajax({
			url: "getFloors.thor",
			cache: false,
			data: {
				floorParam: JSON.stringify(floorParam)
			},
			dataType: 'json', //服务器返回json格式数据
			type: 'post', //HTTP请求类型
			timeout: 10000, //超时时间设置为10秒；
			success: function(data) {
				$('.navListContent ul.ul4 li').not($('.navListContent ul.ul4 li:first')).empty().remove();
				var len = data.length;
				for(var i = 0; i < len; i++) {
					$('.navListContent ul.ul4').append('<li><span>' + data[i].name + '</span><i></i></li>');
					louyengArr[i] = data[i];
				}
			},
			error: function(data) {
				var data = JSON.stringify(data)
				layer.msg("查询失败")
			}
		});
	}
	//-------------------------查询楼层
	//---------------------------查询加载账单数据
	var contract = [];

	function searchBill(storesArr, keyWords, page) {
		var beginSettle = $('#fromYear').html() + $('#fromMonth').html(); //开始时间参数
		var endSettle = $('#toYear').html() + $('#toMonth').html(); //结束时间参数
		var keyWords = $('#search').val() || keyWords || null; //搜索关键字
		var stores = storesArr; //stores
		var userGroups = loginInfo.userGroups; //用户组
		var page = page || 0;
		var queryParam = { //传参
			pageSize: 10,
			page: page,
			beginSettle: beginSettle,
			endSettle: endSettle,
			keyWords: keyWords,
			stores: stores,
			contracts: [],
			userGroups: userGroups
		};

		$.ajax({
			url: "queryByStore.thor",
			cache: false,
			data: {
				queryParam: JSON.stringify(queryParam)
			},
			dataType: 'json', //服务器返回json格式数据
			type: 'post', //HTTP请求类型
			timeout: 10000, //超时时间设置为10秒；
			success: function(data) {
				if(data.result.pageCount == 0) {
					layer.msg('暂时没有数据');
				}
				$('#jiaokuanlvnum').html(data.receiptRate.toPercent());
				$('#yingjiaonum').html(data.payTotal);
				$('#yijiaonum').html(data.payedTotal);
				$('#weijiaonum').html(data.unpayedTotal);
				var recordCount = data.result.recordCount; //查询总记录数
				var records = data.result.records;
				$('#scroller ul.display').empty();
				contract.splice(0, contract.length); //清空contract数组
				for(var i = 0; i < records.length; i++) {
					$('#scroller ul.display').append('<li>' +
						'<dl>' +
						'<dt><img src="../pages/bill/img/bill_no_image_default.png"/></dt>' +
						'<dd>' +
						'<p>' + records[i].tenant.code + ' &nbsp;<span class="billName">' + records[i].tenant.name + '</span></p>' +
						'<div class="yingjiao">' +
						'<p>[应缴]</p>' +
						'<p>' + records[i].payTotal + '元</p>' +
						'</div>' +
						'<div class="line"></div>' +
						'</dd>' +
						'</dl>' +
						'<div class="weijiao">' +
						'<p class="green">[已缴] <span class="yjnum green">' + records[i].payedTotal + '</span>元</p>' +
						'<p class="red">[未缴] <span class="wjnum red">' + records[i].unpayedTotal + '</span>元</p>' +
						'</div>' +
						'<div class="rowLine"></div>' +
						'</li>');
					contract.push(records[i].contract.uuid); //将获取的数据contract.uuid加入数组
				}

			},
			error: function(data) {
				var data = JSON.stringify(data)
				layer.msg("查询失败")

			}
		});
	}
	
	//---------------------------查询加载账单数据
	//--------------------------初始化日期控件
	var opt = {
		preset: 'date', //日期
		theme: 'android-ics light', //皮肤样式
		display: 'modal', //显示方式 
		mode: 'scroller', //日期选择模式
		dateFormat: 'yy-mm', // 日期格式
		setText: '确定', //确认按钮名称
		cancelText: '取消', //取消按钮名籍我
		dateOrder: 'yymm', //面板中日期排列格式
		dayText: '日',
		monthText: '月', //面板中年月文字
		endYear: 2020, //结束年份
		onSelect: function(valueText, inst) {
			var dateArr = valueText.split("-");
			var year = dateArr[0];
			var month = dateArr[1];
			if(timeIndex == 0) {
				$('#fromYear').html(year);
				$('#fromMonth').html(month);
				searchBill(storesArr);
			} else {
				$('#toYear').html(year);
				$('#toMonth').html(month);
				searchBill(storesArr);
			}
		}
	};
	var timeIndex = 0;
	$("#date1").click(function() {
		timeIndex = 0;
		$('#timeshow').mobiscroll('show');
	});
	$("#date2").click(function() {
		timeIndex = 1;
		$('#timeshow').mobiscroll('show');
	});
	$('#timeshow').mobiscroll().date();
	$('#timeshow').mobiscroll(opt);
	//--------------------------初始化日期控件
	//--------------------------搜索框点击事件
	$('#searchImg').click(function() {
		searchBill(storesArr);
	})
	$('#searchDelete').click(function() {
		$('#search').val('');
	})
	//--------------------------搜索框点击事件
	//--------------------------点击进入详情页
	$('#scroller ul.display').on('click', 'li', function() {
		var index = $(this).index();
		var beginSettle = $('#fromYear').html() + $('#fromMonth').html(); //开始时间参数
		var endSettle = $('#toYear').html() + $('#toMonth').html(); //结束时间参数
		var topName = $(this).find('span.billName').html();
		sessionStorage.setItem('topName', topName);
		window.location.href = 'tobillDetail.thor?contract=' + encodeURI(contract[index]) + '&beginSettle=' + encodeURI(beginSettle) + '&endSettle=' + encodeURI(endSettle);
	})
	//--------------------------点击进入详情页
	//--------------是否显示结清账户
	$('#settleBill').change(function() {
		var count = 0;
		if(!$('#settleBill').prop('checked')) {
			$('#scroller ul.display li').each(function() {
				var self = this;
				if(parseInt($(self).find('span.wjnum').text()) <= 0) {
					count += Number($(self).find('span.wjnum').text());
					$(self).hide();
				}
			})
			$('#yijiaonum').html(Number($('#yijiaonum').text()) - count);
		} else {
			$('#scroller ul.display li').each(function() {
				var self = this;
				if(parseInt($(self).find('span.wjnum').text()) <= 0) {
					count += Number($(self).find('span.wjnum').text());
					$(self).show();
				}
			})
			$('#yijiaonum').html(Number($('#yijiaonum').text()) + count);
		}
	})
	//--------------用户退出，清空本地数据
	$('.out').click(function() {
		localStorage.clear();
		window.location.href = 'tologin.thor';
	});
	
})

